#include <ctime>
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>

#include <unistd.h>

typedef struct {
    uint64_t startTime; // 单位S
    uint64_t endTime;
} LoginTime;

typedef struct LoginTimeList {
    LoginTime data;
    struct LoginTimeList *next;
} LoginTimeList;
//初始化一个链表 n为链表节点个数
LoginTimeList *loginTimeListCreate(int n)
{
    if (n < 1) {
        return NULL;
    }

    LoginTimeList *head, *node, *end; //定义头节点，普通节点，尾部节点；
    head = (LoginTimeList *)malloc(sizeof(LoginTimeList)); //分配地址
    head->data.startTime = 0;
    head->data.endTime = 0;
    end = head; //若是空链表，则头尾节点一样
    for (int i = 1; i < n; i++) {
        node = (LoginTimeList *)malloc(sizeof(LoginTimeList));
        node->data.startTime = 0;
        node->data.endTime = 0;
        end->next = node;
        end = node;
    }
    end->next = NULL; //结束创建
    return head;
}

//删除链表
//删除链表的元素也就是把前节点的指针域越过要删除的节点指向下下个节点。
// 即：p->next = q->next;然后放出q节点的空间，即free(q);
void deleteLoginTimeList(LoginTimeList *list)
{
    LoginTimeList *t = list;
    int i = 0;
    while (list) {
        t = list->next;
        free(list);
        std::cout << "i:" << i++ << std::endl;
        list = t;
    }
}

void deleteLoginTimeLimit(LoginTimeList **list, int count)
{
    for (int i = 0; i < count; i++) {
        if (*list) {
            deleteLoginTimeList(*list);
            *list = NULL;
        }
        list++;
    }
}
#include <QTime>
#include <QDebug>
int main(int argc, char **argv)
{
    QTime startTime = QTime::fromString("23:59:59", "hh:mm:ss");
    qInfo() << startTime << ":" << QTime(0, 0) << ":" << startTime.secsTo(QTime(0, 0)) * -1;
    return 0;
    // 获取当前时间
    std::time_t result = std::time(nullptr);
    const auto &tm = std::localtime(&result);
    std::cout << std::asctime(std::localtime(&result))
              << result << " seconds since the Epoch\n";
    std::cout << "tm_wday" << tm->tm_wday << std::endl;

    std::tm t = {};
    std::istringstream ss("01:12:3");
    // ss.imbue(std::locale("de_DE.utf-8"));
    ss >> std::get_time(&t, "%H:%M:%S");
    if (ss.fail()) {
        std::cout << "Parse failed\n";
    } else {
        std::cout << std::put_time(&t, "%T") << '\n';
    }
    std::cout << (std::string("00:12:01") < std::string("00:13:00:01")) << std::endl;

    LoginTimeList *loginTimeLimit[7];
    bool tes = true;
    while (true) {
        int sizeDays = tes ? 7 : 4;
        tes = !tes;
        // 默认应该有7个,周日到周六
        for (int day = 0; day < sizeDays; day++) {
            int size = 4;
            LoginTimeList *loginTimeList = loginTimeListCreate(size);
            loginTimeLimit[day] = loginTimeList;
            // 默认应该有7个,周一到周日time_t
            for (int i = 0; i < size; i++) {
                loginTimeList = loginTimeList->next;
            }
        }

        usleep(10);
        deleteLoginTimeLimit(loginTimeLimit, sizeof(loginTimeLimit) / sizeof(loginTimeLimit[0]));
    }

    return 0;
}